﻿const getDepartment = async () => {
    try {
        let departmentCode = getDepartmentCode();
        let response = await fetch(`/api/department/get-department-info/${departmentCode}`);
        return await response.json();
    } catch (error) {
        console.log('Request Failed', error);
    }
}

const getStation = async () => {
    try {
        let stationCode = getStationCode();
        let response = await fetch(`/api/station/get-station-info/${stationCode}`);
        return await response.json();
    } catch (error) {
        console.log('Request Failed', error);
    }
}

const getDepartmentWaitingInfos = async () => {
    try {
        let departmentCode = getDepartmentCode();
        let groupCode = getGroupCode();
        let response;
        if (groupCode) {
            response = await fetch(`/api/queue/get-waiting-queues/department/${departmentCode}?__group_code=${groupCode}`);
        } else {
            response = await fetch(`/api/queue/get-waiting-queues/department/${departmentCode}`);
        }
        return await response.json();
    } catch (error) {
        console.log('Request Failed', error);
    }
}

const getDepartmentCallingInfos = async () => {
    try {
        let departmentCode = getDepartmentCode();
        let groupCode = getGroupCode();
        let response;
        if (groupCode) {
            response = await fetch(`/api/queue/get-calling-queues/department/${departmentCode}?__group_code=${groupCode}`);
        } else {
            response = await fetch(`/api/queue/get-calling-queues/department/${departmentCode}`);
        }
        return await response.json();
    } catch (error) {
        console.log('Request Failed', error);
    }
}

const getStationWaitingInfos = async () => {
    try {
        let stationCode = getStationCode();
        const response = await fetch(`/api/queue/get-waiting-queues/station/${stationCode}`);
        return await response.json();
    } catch (error) {
        console.log('Request Failed', error);
    }
}

const getStationCallingInfos = async () => {
    try {
        let stationCode = getStationCode();
        const response = await fetch(`/api/queue/get-calling-queues/station/${stationCode}`);
        return await response.json();
    } catch (error) {
        console.log('Request Failed', error);
    }
}

const removeCallingQueue = async id => {
    await fetch(`/api/queue/remove-calling-queue/${id}`, {
        method: 'POST'
    });
};

const wait = second => {
    return new Promise((resolve) => {
        let times = 0;
        const handle = setInterval(() => {
            times++;
            if (times >= second) {
                clearInterval(handle);
                resolve();
            }
        }, 1000);
    });
};

const callPatiens = async (callingInfos) => {
    if (callingInfos instanceof Array) {
        for (const callingInfo of callingInfos) {
            if (callingInfo.callingContent && callingInfo.callingTimes) {
                for (let i = 0; i < callingInfo.callingTimes; i++) {
                    const host = window.chrome?.webview?.hostObjects?.host;
                    if (host) {
                        await host.speak(callingInfo.callingContent);
                    } else {
                        await wait(5);
                    }
                }
            }
            await removeCallingQueue(callingInfo.id);
        }
    }
}

getDepartmentCode = () => {
    const array = window.location.pathname.split('/');
    if (array.length > 1) {
        return array[array.length - 1];
    }
    return null;
};

getStationCode = () => {
    const array = window.location.pathname.split('/');
    if (array.length > 1) {
        return array[array.length - 1];
    }
    return null;
};

getGroupCode = () => {
    var key = '__group_code';
    var reg = new RegExp('[\?\&]' + key + '=([^\&]*)(\&?)', 'i');
    let value = window.location.search.match(reg);
    if (!value) return null;
    return value[1];
};